package com.rbcs.reimb.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.model.Project;
import com.rbcs.base.project.service.ProjectManager;
import com.rbcs.reimb.ReimbConstants;
import com.rbcs.reimb.model.ReimbCount;
import com.rbcs.reimb.service.ReimbCountManager;
import com.systop.common.modules.dept.model.Dept;
import com.systop.common.modules.dept.service.DeptManager;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.Constants;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.JsonCrudAction;

/**
 * 借款报销统计管理Action
 * 
 * @author ShangHua
 */
@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ReimbCountAction extends JsonCrudAction<ReimbCount, ReimbCountManager> {

	// 统计类型
	private String countType;
	
	// 开始时间
	private Date startDate;

	// 结束时间
	private Date endDate;
	

	// 部门管理Manager
	@Autowired
	private DeptManager deptManager;
	
	// 项目管理Manager
	@Autowired
	private ProjectManager projectManager;
	
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		User user = UserUtil.getPrincipal(getRequest());
		Dept loginDept = new Dept();
		double borrowAmount = 0;
		double reimbAmount = 0;
		double totalBorrowAmount = 0;
		double totalReimbAmount = 0;
		List countList = new ArrayList();
		if (StringUtils.isNotBlank(countType)) {
			if (user.getDept() != null) {
				loginDept = getManager().getDao().get(Dept.class, user.getDept().getId());	
			}
			if (countType.equals("1")) {
				ReimbCount reimbCount = new ReimbCount();
				borrowAmount = getManager().getBorrowAmount(user.getId(), null, null, startDate, endDate);
				reimbAmount  = getManager().getReimbAmount(user.getId(), null, null, startDate, endDate);
				reimbCount.setBorrowAmount(borrowAmount);
				reimbCount.setReimbAmount(reimbAmount);
				reimbCount.setUser(user);
				reimbCount.setDept(loginDept);
				countList.add(reimbCount);
				getRequest().setAttribute("totalBorrowAmount", reimbCount.getBorrowAmount());
				getRequest().setAttribute("totalReimbAmount", reimbCount.getReimbAmount());
			} else if (countType.equals("2")) {
				//如果当前登录的用户是部门负责人，则查询本部门以及下属部门的借款报销信息
				if (StringUtils.isNotBlank(user.getType()) && user.getType().equals(Constants.YES)) {
					List<Dept> depts = deptManager.getDeptsBySerNo(loginDept.getSerialNo());
					for (Dept dept : depts) {
						ReimbCount reimbCount = new ReimbCount();
						borrowAmount = getManager().getBorrowAmount(null, dept.getId(), null, startDate, endDate);
						reimbAmount  = getManager().getReimbAmount(null, dept.getId(), null, startDate, endDate);
						reimbCount.setBorrowAmount(borrowAmount);
						reimbCount.setReimbAmount(reimbAmount);
						reimbCount.setDept(dept);
						countList.add(reimbCount);
						totalBorrowAmount = totalBorrowAmount + reimbCount.getBorrowAmount();
						totalReimbAmount = totalReimbAmount + reimbCount.getReimbAmount();
					}
					getRequest().setAttribute("totalBorrowAmount", totalBorrowAmount);
					getRequest().setAttribute("totalReimbAmount", totalReimbAmount);
				}
			}  else if (countType.equals("3")) {
				List<Project> projects = projectManager.getGroupProjects(user.getId());
				for (Project project : projects) {
					ReimbCount reimbCount = new ReimbCount();
					borrowAmount = getManager().getBorrowAmount(null, null, project.getId(), startDate, endDate);
					reimbAmount  = getManager().getReimbAmount(null, null, project.getId(), startDate, endDate);
					reimbCount.setBorrowAmount(borrowAmount);
					reimbCount.setReimbAmount(reimbAmount);
					reimbCount.setProject(project);
					countList.add(reimbCount);
					totalBorrowAmount = totalBorrowAmount + reimbCount.getBorrowAmount();
					totalReimbAmount = totalReimbAmount + reimbCount.getReimbAmount();
				}
				getRequest().setAttribute("totalBorrowAmount", totalBorrowAmount);
				getRequest().setAttribute("totalReimbAmount", totalReimbAmount);
			}
		}
		page.setData(countList);
		restorePageData(page);		
		return INDEX;
	}
	
	/**
	 * 显示统计类型
	 */
	public Map<String, String> getCountTypeMap() {
		return ReimbConstants.COUNT_TYPE_MAP;
	}
	
  /**
   * @return
   */
	public String getCountType() {
		return countType;
	}


	public void setCountType(String countType) {
		this.countType = countType;
	}  
	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}


}